package com.woyou.ui.fragment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.citaq.ideliver.R;
import com.woyou.bean.DeliverTime;
import com.woyou.ui.adapter.SelectTimeAdapter;
import com.woyou.utils.Configure;
import com.woyou.utils.UIUtils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

@EViewGroup(R.layout.include_selecttime)
public class SelectTimeLayout extends LinearLayout implements OnClickListener {
	@ViewById ListView include_selecttime_lv;
	private SelectTimeAdapter selectTimeAdapter;
	@ViewById RelativeLayout include_selecttime_panel;
	@ViewById RelativeLayout include_selecttime_cancel;
	@ViewById RelativeLayout include_selecttime_ok;
	List<DeliverTime> timeList;
	private int MAX_HEIGHT;
	
	public SelectTimeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		int height = Configure.screenHeight;  //得到高度
		MAX_HEIGHT = height - (int)context.getResources().getDimension(R.dimen.dp650);
	}

	/**
	 * 初始化页面
	 */
	@AfterViews
	public void initView() {
		include_selecttime_panel.setOnClickListener(this);
		include_selecttime_cancel.setOnClickListener(this);
		include_selecttime_ok.setOnClickListener(this);
	}

	/**
	 * 刷新数据
	 */
	public void refreshData(List<DeliverTime> timeList){
		setVisibility(View.VISIBLE);
		this.timeList=timeList;
		selectTimeAdapter = new SelectTimeAdapter(getContext(),timeList);
		include_selecttime_lv.setAdapter(selectTimeAdapter);
		try {
			UIUtils.setListViewHeightBasedOnChildren(include_selecttime_lv, MAX_HEIGHT);
		} catch (Exception e) {
		}
	}

	private SelectTimeListener listener;
	public interface SelectTimeListener{
		void selectTime(DeliverTime deliverTime);
	}
	public void setSelectTimeListener(SelectTimeListener l){
		this.listener = l;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.include_selecttime_ok:
			listener.selectTime(selectTimeAdapter.getCheckedDT());
			setVisibility(View.GONE);
			break;
		case R.id.include_selecttime_panel:
		case R.id.include_selecttime_cancel:
			setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * 防止View的事件穿透
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
}
